package com.ikangtai.shecare.common.db.sync;

import android.content.Context;

import com.ikangtai.shecare.common.App;
import com.ikangtai.shecare.common.db.DBManager;
import com.ikangtai.shecare.common.eventBusMsg.sync.SyncRecordInfoMsg;
import com.ikangtai.shecare.common.eventBusMsg.sync.SyncRecordInfoMsg1;
import com.ikangtai.shecare.common.util.AppLog;
import com.ikangtai.shecare.common.util.DateUtil;
import com.ikangtai.shecare.common.util.OKHttpUtil;
import com.ikangtai.shecare.record.bean.UserRecordData;
import com.ikangtai.shecare.record.bean.UserTemperatureInfo;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.simple.eventbus.EventBus;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2016/7/6.
 */
public class SyncRecordInfo {

    private Context context;
    private SyncRecordInfoMsg syncRecordInfoMsg;

    public SyncRecordInfo(Context context) {
        this.context = context;
    }

    public SyncRecordInfo(Context context, SyncRecordInfoMsg syncRecordInfoMsg) {
        this.context = context;
        this.syncRecordInfoMsg = syncRecordInfoMsg;
        EventBus.getDefault().register(this);
    }

    public void syncRecordInfoWithNetwork(){
        final DBManager dbManager = new DBManager(context);

        List<UserRecordData> userRecordDataList = dbManager.getUnSyncedRecordDataList(App.userName);

        if (userRecordDataList.size() == 0){
            AppLog.i("syncRecordInfoWithNetwork no need sync!");
            EventBus.getDefault().post(syncRecordInfoMsg);
        } else {
            JSONArray jsonArray = getUploadJsonArray(userRecordDataList);
            OKHttpUtil okHttpClient = new OKHttpUtil(context);
            String appendUrl = "sync/addSignsRecords.json";

            AppLog.i("syncRecordInfoWithNetwork is syncing ...!");

            try {
                okHttpClient.add("authToken", App.authToken);
                okHttpClient.add("records", jsonArray);
            } catch (JSONException e) {
                e.printStackTrace();
            }

            okHttpClient.postAsync(appendUrl, okHttpClient.new BaseCallBack() {
                @Override
                public void onFailure(Request request, IOException e) {
                    super.onFailure(request, e);
                    EventBus.getDefault().post(syncRecordInfoMsg);
                }
                @Override
                public void onResponse(Response response) throws IOException {
                    int respCode = 0;

                    try {
                        JSONObject respJSONObj = new JSONObject(response.body().string());
                        respCode = respJSONObj.getInt("code");

                        if (App.HTTP_RESP_OK == respCode){
                            dbManager.updateSyncRecordInfoFlag(App.userName);
                            AppLog.i("syncRecordInfoWithNetwork onResponse success!");
                        } else if (respCode == App.HTTP_RESP_TOKEN_TIME_OUT) {
                            OKHttpUtil.setNewToken();
                            syncRecordInfoWithNetwork();
                        } else {
                            AppLog.i("syncRecordInfoWithNetwork onResponse failed! respCode is:" + respCode);
                        }

                        if (respCode != App.HTTP_RESP_TOKEN_TIME_OUT)
                            EventBus.getDefault().post(syncRecordInfoMsg);
                    } catch (JSONException e) {
                        AppLog.i("syncRecordInfoWithNetwork JSONException!");
                        e.printStackTrace();
                        EventBus.getDefault().post(syncRecordInfoMsg);
                    }
                }
            });
        }
    }

    private JSONArray getUploadJsonArray(List<UserRecordData> userRecordDataList) {
        JSONArray jsonArray = new JSONArray();
        for (int i = 0; i < userRecordDataList.size(); i++) {
                jsonArray.put(userRecordDataList.get(i).getUserRecordDataInfoJson());
        }
        return jsonArray;
    }

}